package com.bdzl.ecs.util.electronicUtils;

/*
public class DesUtils {
}
*/


import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;

/**
 * 3DES加密工具类
 */
public class DesUtils {
    //SecretKey负责对称密钥的保存
    private SecretKey desKey;
    //完成加密和解密工作
    private Cipher c;
    //保存加密结果
    private byte[] cipherResultByte;
    private final static String Algorithm = "DESede/ECB/NoPadding";//加密方法／运算模式
    private static DESedeKeySpec dks;
    private static SecretKeyFactory keyFactory;


    public void DESeseTest02() {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        try {
            dks = new DESedeKeySpec("00000000000000000000000000000000".getBytes());
            keyFactory = SecretKeyFactory.getInstance("DESede");
            //生成密钥
            desKey=keyFactory.generateSecret(dks);
            //生成Cipher对象，指定其支持DES算法
            c=Cipher.getInstance(Algorithm);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }

    public byte[] createEncryptor(byte[] b){
        try {
//          System.out.println(str);
            //根据密钥，对Cipher进行初使化，DECRYPT_MODE加密模式
            c.init(Cipher.ENCRYPT_MODE, desKey);
//          byte[] input=str.getBytes();
//          System.out.println(input.length);
            //加密，将结果保存
            cipherResultByte=c.doFinal(b);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return cipherResultByte;
    }
}